package com.controller;

import com.dao.UserDAO;
import com.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
public class RegisterController {
    private UserDAO userDAO;

    public RegisterController(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }

    @PostMapping("/register")
    public String register(@RequestParam String account,
                           @RequestParam String userPwd,
                        @RequestParam String userName,
                        @RequestParam String sex,
                        @RequestParam String introduction,
                        Model model) {
        // 检查手机号是否已存在
        if (userDAO.findStudentByAccount(account) != null) {
            model.addAttribute("error", "该手机号已被注册");
            return "register";
        } else {
            // 创建新学生对象并保存到数据库
            User newUser = new User();
            newUser.setAccount(account);
            newUser.setUserPwd(userPwd);
            newUser.setUserName(userName);
            newUser.setSex(sex);
            newUser.setIntroduction(introduction);
            userDAO.insert(newUser);
            model.addAttribute("success", "注册成功，请登录");
            return "redirect:/login"; // 假设有一个登录页面
        }
    }
}
